<?php
namespace app\admin\controller;
use think\Controller;
use think\captcha\Captcha;
use think\facade\Cookie;

class Login extends Controller
{
    public function login()
    {
    	// cookie初始化
		// Cookie::init(['prefix'=>'think_','expire'=>3600,'path'=>'/']);
		// // 指定当前前缀
		// Cookie::prefix('think_');
		// // // 设置cookie 前缀为think_
		// Cookie::set('name','value',['prefix'=>'think_','expire'=>3600]);
		// // // 获取指定前缀的cookie值
		// $res = Cookie::get('name','think_');
		// dump($res);
		// exit;
		if(session('?admin_id') && session('admin_id')>0){
            $this->error("您已登录",url('Admin/Index/index'));
        }
    	if(request()->isAjax()){
    		// dump(input());exit;
    		$data = input();
            /*if(!captcha_check($data['code'])){
				$json = array('status'=>0,'code'=>1,'message'=>'验证码错误');
				echo json_encode($json);exit;
			};*/
			// $model_admin = new Admin();
			if(!empty($data['username']) && !empty($data['password'])){
				$condition['user_name'] = base64_decode($data['username']);
				$condition['password'] = base64_decode($data['password']);
				$condition['password'] = md5($condition['password']);
				$condition['status'] = 1;
				$admin_info = db('admin')->where($condition)->find();
				if(is_array($admin_info)){
					$auth_group_access = db('auth_group_access')->where(array('uid'=>$admin_info['admin_id']))->find();
                    $auth_group = db('auth_group')->where(array('id'=>$auth_group_access['group_id']))->find();
                    $auth_rule = db('auth_rule')->where('id in ('.$auth_group['rules'].')')->select();
					$rule=[];
                    foreach ($auth_rule as $value) {
					    $rule[]=$value['name'];
                    }
                    session('admin_id',$admin_info['admin_id']);
                    session('user_name',$admin_info['user_name']);
                    session('admin_title',$auth_group['title']);
                    session('rule',$rule);
					if($auth_group['level'] == '1'){
						session('admin_super',1);
					}
					$json = array('status'=>'success','url'=>'/index.php/Admin/Index/index');
					//记录日志开始
					$logdata['admin_id'] = $admin_info['admin_id'];
					$logdata['user_name'] = $admin_info['user_name'];
					$logdata['log_ip'] = getIp();
					$logdata['log_time'] = time();
					$logcon[0]['alt'] = $admin_info['user_name'].'登录成功!';
					$logcon[0]['time'] = time();
					$logdata['content'] = json_encode($logcon);
					$log_id = db('admin_log')->insertGetId($logdata);
					session('log_id',$log_id);
					//记录日志结束
					echo json_encode($json);exit;
				}else{
					$json = array('status'=>0,'message'=>'账号密码不正确');
					echo json_encode($json);exit;
				}
				// dump($admin_info);exit;
				// $admin_info = $model_admin->where($condition)->find();
				// var_dump($admin_info);exit;
			}
    	}
        return view();
    }

    //验证码
    function verify(){
    	ob_clean();
    	$config = [
		'fontSize' => 46,
		'length' => 4,
		'useNoise' => true,
		'codeSet' => "ABCDEFGHIJKLMNOPQRSTUVWXYZ",
		];
		$captcha = new Captcha($config);
		return $captcha->entry();
    }

    /**
     * 退出登录
     * FunctionName: logout
     * @author : roc
     */
    public function logout(){
        session(null);
        $this->success("退出成功",url('login/login'));
    }

}
